package com.xgc.book.bookserver.controller;


import com.xgc.book.bookserver.bean.BookBean;
import com.xgc.book.bookserver.bean.UserinfoBean;
import com.xgc.book.bookserver.service.BookService;
import com.xgc.book.bookserver.service.UserinfoService;
import com.xgc.book.bookserver.util.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 图书表 前端控制器
 * </p>
 *
 * @author wrz
 * @since 2023-11-20
 */
@RestController
@RequestMapping("/book")
public class BookController extends BaseLoginController {
    @Autowired
    private BookService bookService;
    @Autowired
    private UserinfoService userinfoService;

    @GetMapping("/list")
    public ResultJson list() {
        return ResultJson.ok(bookService.list());
    }

    @GetMapping("/page")
    public ResultJson page(int page, int limit, BookBean bookBean) {
        Integer loginId = getLoginId();
        UserinfoBean loginInfo = userinfoService.getById(loginId);
        if (loginInfo.getRole() == 2) {
            bookBean.setAuthorId((getLoginId()));
        }
        return bookService.getPage(page, limit, bookBean);
    }

    @GetMapping("/info/{bookId}")
    public ResultJson info(@PathVariable Integer bookId) {
        return ResultJson.ok(bookService.getById(bookId));
    }

    @PostMapping(value = {"/update", "/status/info", "/status/book"})
    public ResultJson update(@RequestBody BookBean bookBean) {
        if (bookBean.getBookId() == null) {
            bookBean.setAuthorId(getLoginId());
        }
        bookService.update(bookBean);
        return ResultJson.ok();
    }

    @DeleteMapping("/delete/{bookId}")
    public ResultJson delete(@PathVariable Integer bookId) {
        bookService.delete(bookId);
        return ResultJson.ok();
    }

}

